{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b67518a3",
   "metadata": {},
   "source": [
    "<center> <font size=5>十分钟入门 Pandas</font>\n",
    "<p>帮助 Pandas 新手快速上手的简介"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f74fe8d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47ad71d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2538f64e",
   "metadata": {},
   "source": [
    "# 生成对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b27cf3e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用列表生成 Series\n",
    "s = pd.Series([1, 3, 5, np.nan, 6, 8])\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "493bf058",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用 Series、字典、对象生成 DataFrame\n",
    "df2 = pd.DataFrame({'A': 1.,\n",
    "                    'B': pd.Timestamp('20130102'),\n",
    "                    'C': pd.Series(1, index=list(range(4)), dtype='float32'),\n",
    "                    'D': np.array([3] * 4, dtype='int32'),\n",
    "                    'E': pd.Categorical([\"test\", \"train\", \"test\", \"train\"]),\n",
    "                    'F': 'foo'})\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28a54b50",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用含日期时间索引、标签、 NumPy 数组生成 DataFrame\n",
    "d = pd.date_range('20130101', periods=6)\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3712b196",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.randn(6, 4), index=d, columns=list('ABCD'))\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e305f34b",
   "metadata": {},
   "source": [
    "# 查看数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa31aab8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#查看 DataFrame 列的数据类型\n",
    "df2.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62e3f4e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#查看 DataFrame 头部和尾部数据\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9a25713",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.tail(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a640f8d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看索引\n",
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cccf8ffd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#查看列名\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e4fdf98",
   "metadata": {},
   "outputs": [],
   "source": [
    "#查看数据的统计摘要\n",
    "df.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0f1141b",
   "metadata": {},
   "source": [
    "# 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cda3e3f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#按轴排序\n",
    "df.sort_index(axis=1, ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0eac9ccc",
   "metadata": {
    "scrolled": false,
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "#按值排序\n",
    "df.sort_values(by='B')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b1d1f9d",
   "metadata": {},
   "source": [
    "# 选择数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8459f28",
   "metadata": {},
   "outputs": [],
   "source": [
    "#选择单列\n",
    "df['A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0fb5595",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用 [ ] 切片行\n",
    "df[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7898e4ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['2013-01-02':'2013-01-04']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f72f3e2a",
   "metadata": {},
   "source": [
    "## 按标签选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "512b04e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc['2013-01-02']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe2a297b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用标签提取一行数据\n",
    "df.loc[d[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3b308e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用标签选择多列数据\n",
    "df.loc[:, ['A', 'B']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27549830",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用标签切片\n",
    "df.loc['2013-01-02':'2013-01-04', ['A', 'B']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05b253ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "#取某个标签的值\n",
    "df.loc['2013-01-02', 'A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "435df958",
   "metadata": {},
   "outputs": [],
   "source": [
    "#快速某个标签的值\n",
    "df.at['2013-01-02', 'A']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40511fba",
   "metadata": {},
   "source": [
    "## 按位置选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "424f2ddf",
   "metadata": {},
   "outputs": [],
   "source": [
    "#按位置选择\n",
    "df.iloc[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c92ea361",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用整数切片\n",
    "df.iloc[3:5, 0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "188e9c97",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用整数列表按位置切片\n",
    "df.iloc[[1, 2, 4], [0, 2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9930fcb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#整行切片\n",
    "df.iloc[1:3, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fc6598d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#整列切片\n",
    "df.iloc[:, 1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15649537",
   "metadata": {},
   "outputs": [],
   "source": [
    "#显式提取值\n",
    "df.iloc[1, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67b67ca1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#快速访问标量\n",
    "df.iat[1, 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "458def40",
   "metadata": {},
   "source": [
    "## 筛选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f132b66",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[df['B'] > 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c32dbd6",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[df > 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26938b72",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用 isin() 筛选\n",
    "df3 = df.copy()\n",
    "df3['E'] = ['one', 'one', 'two', 'three', 'four', 'three']\n",
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bc883a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "df3[df3['E'].isin(['two', 'four'])]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "779416f9",
   "metadata": {},
   "source": [
    "# 赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8934bf2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用索引自动对齐新增列的数据\n",
    "s1 = pd.Series([1, 2, 3, 4, 5, 6], index=d)\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15c8402a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['F'] = s1\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "906b0726",
   "metadata": {},
   "outputs": [],
   "source": [
    "#按标签赋值\n",
    "df.at['2013-01-01', 'A'] = 0\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3c6d105",
   "metadata": {},
   "outputs": [],
   "source": [
    "#按位置赋值\n",
    "df.iat[0, 2] = 0\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e24bd53f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#按数组赋值\n",
    "df['G'] = [1,2,3,4,5,6]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad97a273",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 按 NumPy 数组赋值\n",
    "df.loc[:, 'D'] = np.array([5] * len(df))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74eccfde",
   "metadata": {},
   "outputs": [],
   "source": [
    "# where 条件赋值\n",
    "df3 = df.copy()\n",
    "df3[df3 < 0] = 0\n",
    "df3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5c2fd52",
   "metadata": {},
   "source": [
    "# 空值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "200f3e3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = df.reindex(index=d[0:4], columns=list(df.columns) + ['E'])\n",
    "df1.loc[d[0]:d[1], 'E'] = 1\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d44b4c1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.dropna(how='any')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7719d18",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.fillna(value=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2a813ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.isna(df1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d21ffc5",
   "metadata": {},
   "source": [
    "# 运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78436403",
   "metadata": {},
   "source": [
    "## 算术运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "c00770c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.586813</td>\n",
       "      <td>0.876275</td>\n",
       "      <td>0.231493</td>\n",
       "      <td>-0.258188</td>\n",
       "      <td>-1.083717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.759017</td>\n",
       "      <td>-0.041309</td>\n",
       "      <td>0.396597</td>\n",
       "      <td>-0.699854</td>\n",
       "      <td>0.365706</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3         4\n",
       "0 -0.586813  0.876275  0.231493 -0.258188 -1.083717\n",
       "1 -0.759017 -0.041309  0.396597 -0.699854  0.365706"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame(np.random.randn(2, 5))\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "fad4341b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.049537</td>\n",
       "      <td>-1.420062</td>\n",
       "      <td>-1.719144</td>\n",
       "      <td>2.194501</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.541337</td>\n",
       "      <td>1.525559</td>\n",
       "      <td>0.568975</td>\n",
       "      <td>-1.186132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.000082</td>\n",
       "      <td>-0.866234</td>\n",
       "      <td>1.179405</td>\n",
       "      <td>-0.628139</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3\n",
       "0  0.049537 -1.420062 -1.719144  2.194501\n",
       "1  0.541337  1.525559  0.568975 -1.186132\n",
       "2 -1.000082 -0.866234  1.179405 -0.628139"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame(np.random.randn(3, 4))\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "b79c7d9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "df1+df2\n",
      "           0         1         2         3   4\n",
      "0 -0.537276 -0.543786 -1.487651  1.936313 NaN\n",
      "1 -0.217679  1.484250  0.965572 -1.885985 NaN\n",
      "2       NaN       NaN       NaN       NaN NaN\n",
      "df1-df2\n",
      "           0         1         2         3   4\n",
      "0 -0.636351  2.296337  1.950637 -2.452689 NaN\n",
      "1 -1.300354 -1.566868 -0.172379  0.486278 NaN\n",
      "2       NaN       NaN       NaN       NaN NaN\n",
      "df1*df2\n",
      "           0         1         2         3   4\n",
      "0 -0.029069 -1.244365 -0.397970 -0.566593 NaN\n",
      "1 -0.410884 -0.063019  0.225654  0.830118 NaN\n",
      "2       NaN       NaN       NaN       NaN NaN\n",
      "df1/df2\n",
      "            0         1         2         3   4\n",
      "0 -11.845840 -0.617069 -0.134656 -0.117652 NaN\n",
      "1  -1.402114 -0.027078  0.697036  0.590030 NaN\n",
      "2        NaN       NaN       NaN       NaN NaN\n"
     ]
    }
   ],
   "source": [
    "print(\"df1+df2\\n\", df1+df2)\n",
    "print(\"df1-df2\\n\", df1-df2)\n",
    "print(\"df1*df2\\n\", df1*df2)\n",
    "print(\"df1/df2\\n\", df1/df2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "831e9539",
   "metadata": {},
   "source": [
    "## 比较操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "3950233c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "df1 等于 df2\n",
      "        0      1      2      3      4\n",
      "0  False  False  False  False  False\n",
      "1  False  False  False  False  False\n",
      "2  False  False  False  False  False\n",
      "df1 不等于 df2\n",
      "       0     1     2     3     4\n",
      "0  True  True  True  True  True\n",
      "1  True  True  True  True  True\n",
      "2  True  True  True  True  True\n",
      "df1 大于 df2\n",
      "        0      1      2      3      4\n",
      "0  False   True   True  False  False\n",
      "1  False  False  False   True  False\n",
      "2  False  False  False  False  False\n",
      "df1 小于 df2\n",
      "        0      1      2      3      4\n",
      "0   True  False  False   True  False\n",
      "1   True   True   True  False  False\n",
      "2  False  False  False  False  False\n",
      "df1 大于等于 df2\n",
      "        0      1      2      3      4\n",
      "0  False   True   True  False  False\n",
      "1  False  False  False   True  False\n",
      "2  False  False  False  False  False\n",
      "df1 小于等于 df2\n",
      "        0      1      2      3      4\n",
      "0   True  False  False   True  False\n",
      "1   True   True   True  False  False\n",
      "2  False  False  False  False  False\n"
     ]
    }
   ],
   "source": [
    "print(\"df1 等于 df2\\n\", df1.eq(df2))\n",
    "print(\"df1 不等于 df2\\n\", df1.ne(df2))\n",
    "print(\"df1 大于 df2\\n\", df1.gt(df2))\n",
    "print(\"df1 小于 df2\\n\", df1.lt(df2))\n",
    "print(\"df1 大于等于 df2\\n\", df1.ge(df2))\n",
    "print(\"df1 小于等于 df2\\n\", df1.le(df2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d727f6ad",
   "metadata": {},
   "source": [
    "## 统计"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "250f9086",
   "metadata": {},
   "source": [
    "<table><thead><tr>\n",
    "<th>函数\n",
    "</th><th>描述\n",
    "</th><th>函数\n",
    "</th><th>描述\n",
    "</th><th>函数\n",
    "</th><th>描述\n",
    "</th>\n",
    "<th>函数\n",
    "</th><th>描述\n",
    "</th>\n",
    "</tr>\n",
    "</thead><tbody><tr>\n",
    "<td><code>count\n",
    "</code>\n",
    "</td><td>统计非空值数量\n",
    "</td>\n",
    "<td><code>sum\n",
    "</code>\n",
    "</td><td>汇总值\n",
    "</td>\n",
    "<td><code>mean\n",
    "</code>\n",
    "</td><td>平均值\n",
    "</td>\n",
    "<td><code>mad\n",
    "</code>\n",
    "</td><td>平均绝对偏差\n",
    "</td>\n",
    "</tr><tr><td><code>median\n",
    "</code>\n",
    "</td><td>算数中位数\n",
    "</td>\n",
    "<td><code>min\n",
    "</code>\n",
    "</td><td>最小值\n",
    "</td>\n",
    "<td><code>max\n",
    "</code>\n",
    "</td><td>最大值\n",
    "</td>\n",
    "<td><code>mode\n",
    "</code>\n",
    "</td><td>众数\n",
    "</td>\n",
    "</tr><tr><td><code>abs\n",
    "</code>\n",
    "</td><td>绝对值\n",
    "</td>\n",
    "<td><code>prod\n",
    "</code>\n",
    "</td><td>乘积\n",
    "</td>\n",
    "<td><code>std\n",
    "</code>\n",
    "</td><td>标准偏差\n",
    "</td>\n",
    "<td><code>var\n",
    "</code>\n",
    "</td><td>无偏方差\n",
    "</td>\n",
    "</tr><tr><td><code>sem\n",
    "</code>\n",
    "</td><td>平均值的标准误差\n",
    "</td>\n",
    "<td><code>skew\n",
    "</code>\n",
    "</td><td>样本偏度 (第三阶)\n",
    "</td>\n",
    "<td><code>kurt\n",
    "</code>\n",
    "</td><td>样本峰度 (第四阶)\n",
    "</td>\n",
    "<td><code>quantile\n",
    "</code>\n",
    "</td><td>样本分位数 (不同 % 的值)\n",
    "</td>\n",
    "</tr><tr><td><code>cumsum\n",
    "</code>\n",
    "</td><td>累加\n",
    "</td>\n",
    "<td><code>cumprod\n",
    "</code>\n",
    "</td><td>累乘\n",
    "</td>\n",
    "<td><code>cummax\n",
    "</code>\n",
    "</td><td>累积最大值\n",
    "</td>\n",
    "<td><code>cummin\n",
    "</code>\n",
    "</td><td>累积最小值\n",
    "</td>\n",
    "</tr>\n",
    "</tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "f8cbd38e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.586813</td>\n",
       "      <td>0.876275</td>\n",
       "      <td>0.231493</td>\n",
       "      <td>-0.258188</td>\n",
       "      <td>-1.083717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.759017</td>\n",
       "      <td>-0.041309</td>\n",
       "      <td>0.396597</td>\n",
       "      <td>-0.699854</td>\n",
       "      <td>0.365706</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3         4\n",
       "0 -0.586813  0.876275  0.231493 -0.258188 -1.083717\n",
       "1 -0.759017 -0.041309  0.396597 -0.699854  0.365706"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "630b7148",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   -0.672915\n",
       "1    0.417483\n",
       "2    0.314045\n",
       "3   -0.479021\n",
       "4   -0.359005\n",
       "dtype: float64"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "a68ba067",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.586813</td>\n",
       "      <td>0.876275</td>\n",
       "      <td>0.231493</td>\n",
       "      <td>-0.258188</td>\n",
       "      <td>-1.083717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.345830</td>\n",
       "      <td>0.834966</td>\n",
       "      <td>0.628090</td>\n",
       "      <td>-0.958041</td>\n",
       "      <td>-0.718011</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3         4\n",
       "0 -0.586813  0.876275  0.231493 -0.258188 -1.083717\n",
       "1 -1.345830  0.834966  0.628090 -0.958041 -0.718011"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.cumsum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b64fbba7",
   "metadata": {},
   "source": [
    "## 合并 concat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a33692b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.concat([df1, df2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "152e1a74",
   "metadata": {},
   "source": [
    "## 连接 join"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e02999f",
   "metadata": {},
   "outputs": [],
   "source": [
    "left = pd.DataFrame({'key': ['foo', 'foo', 'bar'], 'lval': [1, 2, 2]})\n",
    "left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae37010c",
   "metadata": {},
   "outputs": [],
   "source": [
    "right = pd.DataFrame({'key': ['foo', 'foo', 'bar'], 'rval': [3, 4, 5]})\n",
    "right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bff844b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.merge(left, right, on='key')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bbfdcc6",
   "metadata": {},
   "source": [
    "## 追加 Append"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "302e9bfd",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.randn(8, 4), columns=['A', 'B', 'C', 'D'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cf81842",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = df.iloc[3]\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42e6c2c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.append(s, ignore_index=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99a645ca",
   "metadata": {},
   "source": [
    "## 分组 group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e1a566e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',\n",
    "                         'foo', 'bar', 'foo', 'bar'],\n",
    "                   'B': ['one', 'two', 'three', 'four',\n",
    "                         'one', 'two', 'three', 'four'],\n",
    "                   'C': np.random.randn(8),\n",
    "                   'D': np.random.randn(8)})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f2f7929",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.groupby('A').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e652c81a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.groupby(['A','B']).sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7d18287",
   "metadata": {},
   "source": [
    "# 数据透视表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05ce4aaf",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({'A': ['one', 'two', 'three', 'four'] * 3,\n",
    "                   'B': ['A', 'B', 'C'] * 4,\n",
    "                   'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 2,\n",
    "                   'D': np.random.randn(12),\n",
    "                   'E': np.random.randn(12)})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb723cc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.pivot_table(df, values='D', index=['A', 'B'], columns=['C'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46e63678",
   "metadata": {},
   "source": [
    "# 时间序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "062a1cda",
   "metadata": {},
   "outputs": [],
   "source": [
    "#将秒级的数据转换为 5 分钟为频率的数据\n",
    "rng = pd.date_range('1/1/2012', periods=100, freq='S')\n",
    "ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50983260",
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.resample('5Min').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e97f657",
   "metadata": {},
   "outputs": [],
   "source": [
    "#时区表示\n",
    "rng = pd.date_range('3/6/2012 00:00', periods=5, freq='D')\n",
    "ts = pd.Series(np.random.randn(len(rng)), rng)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66f4a2d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "ts_utc = ts.tz_localize('UTC')\n",
    "ts_utc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9496b6cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "ts_utc.tz_convert('US/Eastern')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bccd738",
   "metadata": {},
   "outputs": [],
   "source": [
    "#转换时间段\n",
    "rng = pd.date_range('1/1/2012', periods=5, freq='M')\n",
    "ts = pd.Series(np.random.randn(len(rng)), index=rng)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "482e6628",
   "metadata": {},
   "outputs": [],
   "source": [
    "ps = ts.to_period()\n",
    "ps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24a2953d",
   "metadata": {},
   "outputs": [],
   "source": [
    "ps.to_timestamp()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68286281",
   "metadata": {},
   "source": [
    "# 可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49ad655e",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4f6397f",
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = pd.Series(np.random.randn(1000),\n",
    "               index=pd.date_range('1/1/2000', periods=1000))\n",
    "ts = ts.cumsum()\n",
    "ts.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e74d2d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index,\n",
    "                  columns=['A', 'B', 'C', 'D'])\n",
    "df = df.cumsum()\n",
    "plt.figure()\n",
    "df.plot()\n",
    "plt.legend(loc='best')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b39bd2a",
   "metadata": {},
   "source": [
    "# 数据输入 / 输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6753783",
   "metadata": {},
   "source": [
    "## csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97711059",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('foo.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3974c81a",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.read_csv('foo.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5614103",
   "metadata": {},
   "source": [
    "## HDF5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dca32107",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install tables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad7cc24f",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "df.to_hdf('foo.h5','df')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3154de69",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.read_hdf('foo.h5', 'df')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6f34049",
   "metadata": {},
   "source": [
    "## Excel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0099edb",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install openpyxl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfed489b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel('foo.xlsx', sheet_name='Sheet1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25fc0fc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA'])"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "无",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {
    "height": "462px",
    "width": "374px"
   },
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
